let KEY = ''
let page = 1
let haveadd = 0
let havetraverse = 0
let AllGoodsNum = 0
let Address =  ''
let province = ''
let city = ''
let district = ''
let Alladdress = ''
let longitude = '' //经度
let latitude = '' //纬度
let userlatitude = ''
let userlongtitude = ''
let S = 0
let dbc =  wx.cloud.database().collection('Address')
var allGoods = []
Page({
  data:{
    list:[],
    IMGlist:[],
    IMG:[],
    key:null,
    banner:[]
  },
  onLoad(){
    this.getIMGList()
    this.getadd()
    this.getCIBannerImg()
 },
 getCIBannerImg(){
  wx.cloud.database().collection('banner').doc("3ebc85cb634aba2f00c4f23c041ed70d"
  ).get()
  .then(res=>{
    console.log('获取周边广告图成功',res.data)
   this.setData({
     banner:res.data.circleIMG
   })
  })
  .catch(err=>{
   console.log('获取周边广告图失败',err)
  })
 },
 getKey(e){
  console.log('搜索的内容是：',e.detail.value)
  this.setData({
    key:e.detail.value
  })
  KEY = e.detail.value
},
goSearch(e){
  if(!this.data.key){
    wx.showToast({
      title: '请输入内容',
      icon:'none'
    })
  }else{
  console.log('点击了搜索,跳转页面携带的KEY为:',KEY)
  wx.navigateTo({
    url: '/pages/Search/Search?SearchKey='+KEY+'&T='+0+'&address='+Address+'&array='+encodeURIComponent(JSON.stringify(allGoods))+'&userlatitude='+userlatitude+'&userlongtitude='+userlongtitude,
  })
}
},
 getIMGList(){
  wx.cloud.database().collection('ICON').doc('b69f67c06315e725aaa')
  .get()
  .then(res=>{
    this.setData({
      IMG:res.data.img2
    })
    console.log('图标获取成功：',res)
  })
  .catch(err=>{
   console.log('图标获取失败：',err)
 })
},
// getLL(){
//   let that = this
//  wx.getLocation({
//    type: 'wgs84',
//    success (res) {
//      userlatitude = res.latitude
//      userlongtitude = res.longitude
//      console.log('获取用户纬度成功userlatitude:',userlatitude)
//      console.log('获取用户经度成功userlongitude:',userlongtitude)
// //       that.getAddressGoods(page)
//    }
//   })
// },
 /////////////////////////////////////////
    //完全版获取商品
   //1获取地址
   getadd(){
    wx.cloud.callFunction({
      name:'getAddress',
    })
    .then(res=>{
      console.log('res:',res)
      if(res.result.data.length == 0){
        wx.showToast({
          title: '请先选择您的位置',
          icon:'none',
         })
        }else{
     Address = res.result.data[0].address
     console.log('云函数获取地址成功：',Address)
      province = res.result.data[0].province
      city = res.result.data[0].city
      district = res.result.data[0].district
      userlatitude = res.result.data[0].latitude
      console.log('云函数获取userlatitude成功：',userlatitude)
      console.log('云函数获取userlongtitude成功：',userlongtitude)
      userlongtitude = res.result.data[0].longitude
     this.setData({
       locationName: Address?Address: ""
     })
    }
    this.ComparedAddress(province,city,district)
    // this.getLL()
    })
    .catch(err=>{
      console.log('云函数获取地址失败：',err)
    })
   },
   //2根据地址初次筛选商品
   ComparedAddress(province,city,district){
     wx.cloud.callFunction({
       name:'ComparedAddress',
       data:{
        province:province,
        city:city,
        district:district
       }
     })
     .then(res=>{
       console.log('---------周边所有商品获取成功----------:',res)
       allGoods = res.result
       this.getSkip(allGoods,page,havetraverse)
     })
   },
   //3判断是否到底，否的话分页加载商品，一次渲染20条
   IsOver(goods,havetraverse){
    console.log('---------------------')
    console.log('havetraverse',havetraverse)
    console.log('goods.length:',goods.length)
    console.log('---------------------')
    if(havetraverse==goods.length){
      console.log('if判断执行了')
      wx.showToast({
        title: '已经到达底部',
        icon:'none'
      })
     } else {
      this.getSkip(allGoods,havetraverse)
     }
   },
   getSkip(goods,loaded){
    AllGoodsNum = goods.length
    console.log('AllGoodsNum:',AllGoodsNum)
    if(loaded!=AllGoodsNum){
      console.log('havetraverse=AllGoodsNum')
    }
    if(havetraverse!=AllGoodsNum){
      // console.log('for循环执行了')
     for (let index = havetraverse; index < goods.length; index++) {
       const element = goods[index];
       this.getDistance(userlatitude,userlongtitude,element.latitude,element.longitude)
       if(S<=3){
       this.setData({
         list:this.data.list.concat(element)
       })
       haveadd++
      }
      havetraverse++
      if(haveadd==(page+1)*10)
      {
        console.log('数据到达20条，跳出')
        break
      }
      console.log('-------------------------------')
      console.log('已渲染出的的haveadd:',haveadd)
      console.log('数据已遍历的havetraverse:',havetraverse)
      console.log('-------------------------------')
     }
   }
   },
   //下拉触底
   onReachBottom(){
    console.log('---------------触底执行了----------------')
    if(havetraverse==AllGoodsNum){
      wx.showToast({
        title: '已经到达底部',
        icon:'none'
      })
    } else {
      page++
      console.log('---------------触底判断执行了----------------')
      this.getSkip(allGoods,havetraverse)
    }
    console.log('渲染的页面是：',page)
  },
   //---------------------------------
 Rad: function(d) { //根据经纬度判断距离
    return d * Math.PI / 180.0;
},
getDistance: function(lat1, lng1, lat2, lng2) {
    // lat1用户的纬度
    // lng1用户的经度
    // lat2商家的纬度
    // lng2商家的经度
    var radLat1 = this.Rad(lat1);
    var radLat2 = this.Rad(lat2);
    var a = radLat1 - radLat2;
    var b = this.Rad(lng1) - this.Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    S = Math.round(s * 10000) / 10000
    s = s.toFixed(2) + 'km' //保留两位小数
    S = S.toFixed(2)
    console.log('经纬度计算的距离:' + s)
    return s
},

 goDetail(e){
   console.log('点击了商品详情页',e.currentTarget.dataset.id)
   wx.navigateTo({
     url: '/pages/details/details?id='+e.currentTarget.dataset.id,
   })
 },
  //功能页
  goFunction(Type1,Type2){
    wx.navigateTo({
      url: '/pages/function/function?type1='+Type1+'&type2='+Type2+'&userlatitude='+userlatitude+'&userlongtitude='+userlongtitude,
    })
    console.log('成功跳转到功能页面，携带的type值为：',Type1,Type2)
  },
  type1(){ this.goFunction(1,9)},
  type2(){ this.goFunction(10,13)},
  type3(){ this.goFunction(14,17)},
  type4(){ this.goFunction(18,21)},
  type5(){ this.goFunction(22,27)},
  type6(){ this.goFunction(28,30)},
  type7(){ this.goFunction(31,34)},
  type8(){ this.goFunction(35,40)},
  type9(){ this.goFunction(41,44)},
  type10(){ this.goFunction(45,47)},
  type11(){ this.goFunction(48,50)},
  type12(){ this.goFunction(51,52)},

})